package com.virtualtrainer_01.dao;

import android.content.Context;

public class SQLiteWrite {

	Context context;

	public SQLiteWrite(Context c) {
		context = c;
	}

	private void CreateExercise() {
		ExerciseDB entry = new ExerciseDB(context);
		String sql = "CREATE TABLE IF NOT EXISTS " + ExerciseDB.DATABASE_TABLE
				+ " (" + ExerciseDB.KEY_ID
				+ " INTEGER PRIMARY KEY NOT NULL UNIQUE , "
				+ ExerciseDB.KEY_NAME + " VARCHAR NOT NULL UNIQUE, "
				+ ExerciseDB.KEY_ID_MUSCLE + " INTEGER, "
				+ ExerciseDB.KEY_CALORIES + " FLOAT, "
				+ ExerciseDB.KEY_DESCRIPTION + " VARCHAR," + ExerciseDB.KEY_REPS
				+ " INTEGER," + ExerciseDB.KEY_SETS + " INTEGER," + ExerciseDB.KEY_DIFFICULTY+" INTEGER);";
		entry.open();
		entry.commit(sql);
		entry.close();
	}

	private void CreateMuscle() {
		MuscleDB entry = new MuscleDB(context);
		String sql = "CREATE TABLE IF NOT EXISTS " + MuscleDB.DATABASE_TABLE
				+ " (" + MuscleDB.KEY_ID
				+ " INTEGER PRIMARY KEY  NOT NULL UNIQUE , "
				+ MuscleDB.KEY_NAME + " VARCHAR NOT NULL UNIQUE, "
				+ MuscleDB.KEY_ID_GROUP + " INTEGER, "
				+ MuscleDB.KEY_DESCRIPTION + " VARCHAR);";
		entry.open();
		entry.commit(sql);
		entry.close();
	}

	private void CreateGroup() {
		GroupDB entry = new GroupDB(context);
		String sql = "CREATE TABLE IF NOT EXISTS '" + GroupDB.DATABASE_TABLE
				+ "' (" + GroupDB.KEY_ID
				+ " INTEGER PRIMARY KEY NOT NULL UNIQUE , " + GroupDB.KEY_NAME
				+ " VARCHAR NOT NULL UNIQUE, " + GroupDB.KEY_TYPE
				+ " VARCHAR , " + GroupDB.KEY_DESCRIPTION + " VARCHAR);";
		entry.open();
		entry.commit(sql);
		entry.close();
	}

	private void CreateTraining() {
		TrainingDB entry = new TrainingDB(context);
		String sql = "CREATE TABLE IF NOT EXISTS " + TrainingDB.DATABASE_TABLE
				+ " (" + TrainingDB.KEY_ID
				+ " INTEGER PRIMARY KEY NOT NULL UNIQUE , "
				+ TrainingDB.KEY_NAME + " VARCHAR, " + TrainingDB.KEY_TIME
				+ " DATETIME , " + TrainingDB.KEY_DURATION + " VARCHAR );";
		entry.open();
		entry.commit(sql);
		entry.close();
	}

	private void CreateTrainingExercise() {
		TrainingExerciseDB entry = new TrainingExerciseDB(context);
		String sql = "CREATE TABLE IF NOT EXISTS "
				+ TrainingExerciseDB.DATABASE_TABLE + " ("
				+ TrainingExerciseDB.KEY_ID
				+ " INTEGER PRIMARY KEY NOT NULL UNIQUE , "
				+ TrainingExerciseDB.KEY_IDEXERCISE + " INTEGER NOT NULL , "
				+ TrainingExerciseDB.KEY_IDTRAINING + " INTEGER NOT NULL );";
		entry.open();
		entry.commit(sql);
		entry.close();
	}

	private void CreateTrainingGroup() {
		TrainingGroupDB entry = new TrainingGroupDB(context);
		String sql = "CREATE TABLE IF NOT EXISTS "
				+ TrainingGroupDB.DATABASE_TABLE + " ("
				+ TrainingGroupDB.KEY_ID
				+ " INTEGER PRIMARY KEY NOT NULL UNIQUE , "
				+ TrainingGroupDB.KEY_IDGROUP + " INTEGER NOT NULL , "
				+ TrainingGroupDB.KEY_IDTRAINING + " INTEGER NOT NULL );";
		entry.open();
		entry.commit(sql);
		entry.close();
	}

	private void CreateTrainingDay() {
		TrainingDayDB entry = new TrainingDayDB(context);
		String sql = "CREATE TABLE IF NOT EXISTS "
				+ TrainingDayDB.DATABASE_TABLE + " (" + TrainingDayDB.KEY_ID
				+ " INTEGER PRIMARY KEY NOT NULL UNIQUE , "
				+ TrainingDayDB.KEY_IDTRAINING + " INTEGER NOT NULL , "
				+ TrainingDayDB.KEY_BREACKFAST + " INTEGER NOT NULL ,"
				+ TrainingDayDB.KEY_LUNCH + " INTEGER NOT NULL, "
				+ TrainingDayDB.KEY_DINNER + " INTEGER NOT NULL);";
		entry.open();
		entry.commit(sql);
		entry.close();

	}

	private void CreateAliment() {
		AlimentDB entry = new AlimentDB(context);
		String sql = "CREATE TABLE IF NOT EXISTS 'aliment' ("
				+ AlimentDB.KEY_ID + " INTEGER PRIMARY KEY NOT NULL UNIQUE , "
				+ AlimentDB.KEY_NAME + " VARCHAR NOT NULL UNIQUE, "
				+ AlimentDB.KEY_AMOUNT + " FLOAT , " + AlimentDB.KEY_PRICE
				+ " FLOAT);";
		entry.open();
		entry.commit(sql);
		entry.close();
	}

	private void CreateFood() {
		FoodDB entry = new FoodDB(context);
		String sql = "CREATE TABLE IF NOT EXISTS 'food' (" + FoodDB.KEY_ID
				+ " INTEGER PRIMARY KEY NOT NULL UNIQUE , " + FoodDB.KEY_NAME
				+ " VARCHAR NOT NULL UNIQUE, " + FoodDB.KEY_CALORIES
				+ " FLOAT , " + FoodDB.KEY_DESCRIPTION + " VARCHAR);";
		entry.open();
		entry.commit(sql);
		entry.close();
	}

	private void CreateFoodAliment() {
		FoodAlimentDB entry = new FoodAlimentDB(context);
		String sql = "CREATE TABLE IF NOT EXISTS 'foodaliment' ("
				+ FoodAlimentDB.KEY_ID
				+ " INTEGER PRIMARY KEY NOT NULL UNIQUE , "
				+ FoodAlimentDB.KEY_IDFOOD + " INTEGER , "
				+ FoodAlimentDB.KEY_IDALIMENT + " INTEGER );";
		entry.open();
		entry.commit(sql);
		entry.close();
	}

	public void CreateClient() {
		ClientDB entry = new ClientDB(context);
		String sql = "CREATE TABLE IF NOT EXISTS " + ClientDB.DATABASE_TABLE
				+ " (" + ClientDB.KEY_ID
				+ " INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE , "
				+ ClientDB.KEY_NAME + " VARCHAR NOT NULL UNIQUE, "
				+ ClientDB.KEY_DISPONIBILITY + " INTEGER, "
				+ ClientDB.KEY_BIRTHDAY + " VARCHAR, " + ClientDB.KEY_HEIGHT
				+ " FLOAT, " + ClientDB.KEY_WEIGHT + " FLOAT, "
				+ ClientDB.KEY_GENRE + " BOOL);";
		entry.open();
		entry.commit(sql);
		entry.close();
	}

	public void populate() {

		PopulateDatabase pd = new PopulateDatabase();// asta ar trebui sa aduca
														// de pe server
		pd.populate(); // functia care populeaza listele de pe server

		CreateAliment();
		AlimentDB adb = new AlimentDB(context);
		adb.open();
		for (int i = 0; i < pd.alimentList.size(); i++)
			adb.createEntry(pd.alimentList.get(i));
		adb.close();

		CreateFood();
		FoodDB fdb = new FoodDB(context);
		fdb.open();
		for (int i = 0; i < pd.foodList.size(); i++)
			fdb.createEntry(pd.foodList.get(i));
		fdb.close();

		CreateFoodAliment();
		FoodAlimentDB fadb = new FoodAlimentDB(context);
		fadb.open();
		for (int i = 0; i < pd.foodList.size(); i++)
			fadb.createEntry(pd.foodList.get(i));
		fadb.close();

		CreateGroup();
		GroupDB gdb = new GroupDB(context);
		gdb.open();
		for (int i = 0; i < pd.groupList.size(); i++)
			gdb.createEntry(pd.groupList.get(i));
		gdb.close();

		CreateMuscle();
		MuscleDB mdb = new MuscleDB(context);
		mdb.open();
		for (int i = 0; i < pd.muscleList.size(); i++)
			mdb.createEntry(pd.muscleList.get(i));
		mdb.close();

		CreateExercise();
		ExerciseDB edb = new ExerciseDB(context);
		edb.open();
		for (int i = 0; i < pd.exerciseList.size(); i++)
			edb.createEntry(pd.exerciseList.get(i));
		edb.close();

		CreateTraining();
		CreateTrainingExercise();
		CreateTrainingGroup();
		
		//Trebuie salvate, dar mai incolo
		
	//	TrainingDB tdb = new TrainingDB(context);
	//	TrainingExerciseDB ted = new TrainingExerciseDB(context);
	//	TrainingGroupDB tgd = new TrainingGroupDB(context);
	//	tdb.open();

	//	for (int i = 0; i < pd.trainingList.size(); i++) {
	//		tdb.createEntry(pd.trainingList.get(i));
	//		ted.open();

//			ted.createEntry(((Training) (pd.trainingList.get(i))));
	//		ted.close();

	//		tgd.open();
	//		tgd.createEntry(((Training) (pd.trainingList.get(i)))); // pe aici
	//		tgd.close();

		//}

		//tdb.close();
	//	tgd.close();

		CreateTrainingDay();
		//TrainingDayDB tddb = new TrainingDayDB(context);
		//tddb.open();
		//for (int i = 0; i < pd.trainingDayList.size(); i++)
		//	tddb.createEntry(pd.trainingDayList.get(i));
		//tddb.close();
	}
}
